import { readConfiguration } from './function/function.js'

export default class Button {
    constructor() {
        this.plugin = {
            name: 'Bread-Shop',
            dsc: '面包商店',
            priority: 1,
            rule: [
                { reg: /^[#\/][买吃啃送抢赌].*/, fnc: 'controls' },
                { reg: /^[#\/](.*)记录(买|[吃啃]|抢|(赠)?送|猜拳|赌)?/, fnc: 'breadRecord' },
                { reg: /^[#\/]查看.*/, fnc: 'viewBread' },
                { reg: /^[#\/]设置本群自定义物品.*/, fnc: 'setCustomItems' },
                { reg: /^[#\/]?(面包(商店)?|Bs)(菜单|帮助)$/, fnc: 'help' }
            ]
        }
    }

    async controls(e) {
        const ID = [e.group_id]
        const USER_INPUT_CONTENT = r(e.msg)
        const configData = await readConfiguration()
        let defaultItem = configData['bread_thing']
        const CUSTOM_ITEMS = configData['special_thing_group'][ID[0]]
        if (!CUSTOM_ITEMS && USER_INPUT_CONTENT !== defaultItem || CUSTOM_ITEMS && USER_INPUT_CONTENT !== CUSTOM_ITEMS) return false
        if (CUSTOM_ITEMS) defaultItem = CUSTOM_ITEMS

        const buttonList = [
            { label: `买${defaultItem}`, data: `#买${defaultItem}`, enter: true },
            { label: `吃${defaultItem}`, data: `#吃${defaultItem}`, enter: true },
            { label: `抢${defaultItem}`, data: `#抢${defaultItem}`, enter: true },
            { label: `送${defaultItem}`, data: `#送${defaultItem}`, enter: true },
            { label: `赌石头`, data: `#赌${defaultItem}石头`, enter: true },
            { label: `赌剪刀`, data: `#赌${defaultItem}剪刀`, enter: true },
            { label: `赌布`, data: `#赌${defaultItem}布`, enter: true },
            { label: `${defaultItem}榜`, data: `#${defaultItem}排行榜`, enter: true },
            { label: `${defaultItem}全服榜`, data: `#${defaultItem}全局排行榜`, enter: true },
            { label: `操作记录`, data: `#${defaultItem}记录`, enter: true },
            { label: `查看${defaultItem}`, data: `#查看${defaultItem}`, enter: true },
            { label: '设置', data: '#设置本群自定义物品', enter: true }
        ]

        return Bot.Button(buttonList)
    }

    async breadRecord(e) {
        const ID = [e.group_id]
        const [, , USER_INPUT_CONTENT, OPERATION] = e.msg.match(/^(#|\/)(.*)记录(买|[吃啃]|抢|(赠)?送|猜拳|赌)?$/)
        const configData = await readConfiguration()
        let defaultItem = configData['bread_thing']
        const CUSTOM_ITEMS = configData['special_thing_group'][ID[0]]
        if (!CUSTOM_ITEMS && USER_INPUT_CONTENT !== defaultItem || CUSTOM_ITEMS && USER_INPUT_CONTENT !== CUSTOM_ITEMS) return false
        if (CUSTOM_ITEMS) defaultItem = CUSTOM_ITEMS

        const buttonList = [
            { label: `#${defaultItem}记录买`, data: `#${defaultItem}记录买`, enter: true },
            { label: `#${defaultItem}记录吃`, data: `#${defaultItem}记录吃`, enter: true },
            { label: `#${defaultItem}记录抢`, data: `#${defaultItem}记录抢`, enter: true },
            { label: `#${defaultItem}记录送`, data: `#${defaultItem}记录送`, enter: true },
            { label: `#${defaultItem}记录赌`, data: `#${defaultItem}记录赌`, enter: true }
        ]

        return Bot.Button(buttonList)
    }

    async viewBread(e) {
        const ID = [e.group_id]
        const USER_INPUT_CONTENT = r(e.msg)
        const configData = await readConfiguration()
        let defaultItem = configData['bread_thing']
        const CUSTOM_ITEMS = configData['special_thing_group'][ID[0]]
        if (!CUSTOM_ITEMS && USER_INPUT_CONTENT !== defaultItem || CUSTOM_ITEMS && USER_INPUT_CONTENT !== CUSTOM_ITEMS) return false
        if (CUSTOM_ITEMS) defaultItem = CUSTOM_ITEMS

        const buttonList = [
            { label: `查看${defaultItem}`, data: `#查看${defaultItem}`, enter: true },
            { label: `设置个人昵称`, data: `#设置${defaultItem}店昵称`, enter: true }
        ]

        return Bot.Button(buttonList)
    }

    async setCustomItems() {
        const buttonList = [
            { label: '#面包帮助', data: '#面包帮助', enter: true }
        ]

        return Bot.Button(buttonList)
    }

    async help(e) {
        const ID = [e.group_id]
        const configData = await readConfiguration()
        let defaultItem = configData['bread_thing']
        const CUSTOM_ITEMS = configData['special_thing_group'][ID[0]]
        if (CUSTOM_ITEMS) defaultItem = CUSTOM_ITEMS

        const buttonList = [
            { label: `买${defaultItem}`, data: `#买${defaultItem}`, enter: true },
            { label: `吃${defaultItem}`, data: `#吃${defaultItem}`, enter: true },
            { label: `抢${defaultItem}`, data: `#抢${defaultItem}`, enter: true },
            { label: `送${defaultItem}`, data: `#送${defaultItem}`, enter: true },
            { label: `赌石头`, data: `#赌${defaultItem}石头`, enter: true },
            { label: `赌剪刀`, data: `#赌${defaultItem}剪刀`, enter: true },
            { label: `赌布`, data: `#赌${defaultItem}布`, enter: true },
            { label: `${defaultItem}榜`, data: `#${defaultItem}排行榜`, enter: true },
            { label: `${defaultItem}全服榜`, data: `#${defaultItem}全局排行榜`, enter: true },
            { label: `操作记录`, data: `#${defaultItem}记录`, enter: true },
            { label: `查看${defaultItem}`, data: `#查看${defaultItem}`, enter: true }
        ]

        return Bot.Button(buttonList)
    }
}

/**
 * 按钮
 * @param {number} TYPE 0: 立即发送 | 1: 不立即发送 | 2: 直接发送外显 | 3: 链接
 * @param {string} $1 外显
 * @param {string} $2 数据
 * @returns {object}
 */
function r(msg) {
    return msg.replace(/#|\/|买|吃|啃|送|赌|抢|查看/g, '')
}
